package com.chefu.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chefu.admin.pojo.PartProvider;
import com.chefu.carowner.pojo.CarUser;
import com.chefu.carowner.pojo.UserReceiveAddress;
import com.chefu.client.carowner.CarOwnerClient;
import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.order.mapper.AutoPartsOrderDetailMapper;
import com.chefu.order.mapper.AutoPartsOrderMapper;
import com.chefu.order.pojo.AutoPartsOrder;
import com.chefu.order.pojo.AutoPartsOrderDetail;
import com.chefu.order.pojo.vo.*;
import com.chefu.order.service.AutoPartsOrderService;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@Service
public class AutoPartsOrderServiceImpl extends ServiceImpl<AutoPartsOrderMapper, AutoPartsOrder> implements AutoPartsOrderService {

    @Autowired
    private AutoPartsOrderMapper partsOrderMapper;

    @Autowired
    private AutoPartsOrderDetailMapper detailMapper;

    @Override
    public PageResult<AutoPartsOrderPageVo> getOrderPage(Integer pageNo, Integer pageSize, AutoPartsOrderPageParamVo paramVo) {
        PageResult<AutoPartsOrderPageVo> result = new PageResult<>();
        Page<AutoPartsOrder> autoPartsOrderPage = new Page<>(pageNo, pageSize);
        QueryWrapper<AutoPartsOrder> queryWrapper = new QueryWrapper<>();

        if (paramVo.getOrderId() != null) {
            queryWrapper.eq("apo_id", paramVo.getOrderId());
        }
        if (!StringUtils.isEmpty(paramVo.getUserName())) {
            queryWrapper.like("user_name", paramVo.getUserName());
        }
        if (paramVo.getStatus() != null) {
            queryWrapper.eq("order_status", paramVo.getStatus());
        }
        if (paramVo.getlDate() != null) {
            if (paramVo.getrDate() != null) {
                if (paramVo.getlDate().getTime() < paramVo.getrDate().getTime()) {
                    queryWrapper.between("create_time", paramVo.getlDate(), paramVo.getrDate());
                } else {
                    throw new CfException(ExceptionEnum.TIME_INTERVAL_INCORRECT);
                }
            } else {
                queryWrapper.ge("create_time", paramVo.getlDate());
            }
        } else {
            if (paramVo.getrDate() != null) {
                queryWrapper.le("create_time", paramVo.getrDate());
            }
        }
        if (!StringUtils.isEmpty(paramVo.getPartProName())) {
            queryWrapper.like("part_pro_name", paramVo.getPartProName());
        }

        partsOrderMapper.selectPage(autoPartsOrderPage, queryWrapper);
        result.setTotal(autoPartsOrderPage.getTotal());
        result.setTotalPage(autoPartsOrderPage.getPages());
        List<AutoPartsOrder> records = autoPartsOrderPage.getRecords();

        List<AutoPartsOrderPageVo> items = new ArrayList<>();

        for (AutoPartsOrder record : records) {
            AutoPartsOrderPageVo autoPartsPageVo = new AutoPartsOrderPageVo();
            BeanUtils.copyProperties(record, autoPartsPageVo);

            autoPartsPageVo.setStatus(record.getOrderStatus());
            autoPartsPageVo.setCreatTime(record.getCreateTime());

            items.add(autoPartsPageVo);
        }
        result.setItems(items);

        return result;
    }

    @Override
    public AutoPartsDetailVo getOrderDetail(Long orderId) {
        AutoPartsDetailVo autoPartsDetailVo = new AutoPartsDetailVo();
        AutoPartsOrder autoPartsOrder = partsOrderMapper.selectById(orderId);

        autoPartsDetailVo.setApoId(orderId);
        autoPartsDetailVo.setCreateTime(autoPartsOrder.getCreateTime());
        autoPartsDetailVo.setPostStatus(Integer.valueOf(autoPartsOrder.getPostStatus()));

        Long addressId = autoPartsOrder.getAddressId();
        Long cuid = autoPartsOrder.getCuid();
        Long ppId = autoPartsOrder.getPpId();

        //TODO 根据地址id、用户id查询
        // CarOwnerClient
        CarUser carUser = new CarUser();
        UserReceiveAddress address = new UserReceiveAddress();

        ConsigneeVo consigneeVo = new ConsigneeVo();
        consigneeVo.setName(address.getReceiveName());

        Long aId = address.getAId();
        //TODO 根据Aid（省市区id）查询
        consigneeVo.setArea("广东 广州 天河");

        consigneeVo.setContactNumber(address.getPhone());
        consigneeVo.setDeliveryMethod("仓库直发");
        consigneeVo.setPaymentMethod("线上");

        autoPartsDetailVo.setConsignee(consigneeVo);

        ShipperVo shipperVo = new ShipperVo();

        //TODO 根据配件商id查询
        PartProvider partProvider = new PartProvider();

        shipperVo.setName(partProvider.getShortName());

        //TODO 根据Aid（省市区id）查询
        Long areaId = partProvider.getAreaId();
        shipperVo.setArea("河南 焦作");
        shipperVo.setDeliveryAddress(partProvider.getAddress());
        shipperVo.setContactNumber(partProvider.getPhone());

        autoPartsDetailVo.setShipper(shipperVo);

        List<AutoPartsOrderDetail> detailList = detailMapper.selectList(new QueryWrapper<AutoPartsOrderDetail>().eq("apo_id", orderId));

        List<PartOrderDetailVo> detailVoList = new ArrayList<>();
        List<Long> autoPartsIdList = new ArrayList<>();

        for (AutoPartsOrderDetail detail : detailList) {
            PartOrderDetailVo detailVo = new PartOrderDetailVo();
            detailVo.setApId(detail.getApId());
            detailVo.setApoId(orderId);
            detailVo.setApPrice(detail.getUnitPrice());
            detailVo.settNumber(detail.getTNumber());
            detailVo.setPrice(detail.getPrice());
            detailVoList.add(detailVo);
            autoPartsIdList.add(detail.getApId());
        }

        //TODO 根据autoPartsIdList查询配件列表

        List<OrderLogVo> orderLogVoList = new ArrayList<>();
        OrderLogVo orderLogVo = new OrderLogVo();
        orderLogVo.setCreateTime(new DateTime(autoPartsOrder.getCreateTime()).plusDays(1).toDate());
        orderLogVo.setDesc("订单审核");
        orderLogVo.setStatus(Integer.valueOf(autoPartsOrder.getPostStatus()));
        orderLogVoList.add(orderLogVo);

        autoPartsDetailVo.setOrderLogVoList(orderLogVoList);

        return autoPartsDetailVo;
    }
}
